﻿
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Routing;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Components.Alerts;

namespace Allin.Abp.AntDesignUI.Components
{
	public partial class PageAlert : ComponentBase, IDisposable
	{
		private List<AlertWrapper> Alerts = new List<AlertWrapper>();

		[Inject]
		protected IAlertManager AlertManager { get; set; } = default!;

		[Inject]
		protected NavigationManager NavigationManager { get; set; } = default!;

		protected override void OnInitialized()
		{
			base.OnInitialized();
			NavigationManager.LocationChanged += NavigationManager_LocationChanged;
			AlertManager.Alerts.CollectionChanged += Alerts_CollectionChanged;

			Alerts.AddRange(AlertManager.Alerts.Select(t => new AlertWrapper
			{
				AlertMessage = t,
				IsVisible = true
			}));
		}

		//Since Blazor WASM doesn't support scoped dependency, we need to clear alerts on each location changed event.
		private void NavigationManager_LocationChanged(object? sender, LocationChangedEventArgs e)
		{
			AlertManager.Alerts.Clear();
			Alerts.Clear();
		}

		private void Alerts_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
		{
			if (e.Action == NotifyCollectionChangedAction.Add)
			{
				foreach (var item in e.NewItems!)
				{
					Alerts.Add(new AlertWrapper
					{
						AlertMessage = (AlertMessage)item,
						IsVisible = true
					});
				}
			}
			InvokeAsync(StateHasChanged);
		}

		protected virtual string GetAlertType(AlertType alertType)
		{
			var type = alertType switch
			{
				AlertType.Info => AntDesign.AlertType.Info,
				AlertType.Success => AntDesign.AlertType.Success,
				AlertType.Warning => AntDesign.AlertType.Warning,
				AlertType.Danger => AntDesign.AlertType.Error,
				AlertType.Dark => AntDesign.AlertType.Info,
				AlertType.Default => AntDesign.AlertType.Info,
				AlertType.Light => AntDesign.AlertType.Info,
				AlertType.Primary => AntDesign.AlertType.Info,
				AlertType.Secondary => AntDesign.AlertType.Info,
				_ => AntDesign.AlertType.Info,
			};

			return type;
		}

		public void Dispose()
		{
			NavigationManager.LocationChanged -= NavigationManager_LocationChanged;
			AlertManager.Alerts.CollectionChanged -= Alerts_CollectionChanged;
		}
	}
}
